package com.guli.gulimall.gulimallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guli.gulimall.gulimallproduct.entity.ProductAttrValueEntity;
import com.guli.gulimall.gulimallproduct.service.ProductAttrValueService;
import com.guli.gulimall.gulimallproduct.vo.AttrRespVo;
import com.guli.gulimall.gulimallproduct.vo.AttrVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.guli.gulimall.gulimallproduct.service.AttrService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;


/**
 * 商品属性
 *
 * @author zouzhi
 * @email 1245268075@qq.com
 * @date 2022-04-03 10:19:09
 */

@Api(value = "AttrController类接口",tags = "商品属性接口")
@RestController
@RequestMapping("gulimallproduct/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;
    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    @GetMapping("base/listforspu/{spuId}")
    @ApiOperation(value = "根据销售类型spuid查询商品规格参数", notes = "根据销售类型spuid查询商品规格参数")
    public R listforspu(@RequestParam(required = false) Map<String, Object> params, @PathVariable String spuId){
        List<ProductAttrValueEntity> res = productAttrValueService.listforspuByspuId(spuId);
        return R.ok().put("data", res);
    }

    @GetMapping("{attrType}/list/{catId}")
    @ApiOperation(value = "根据商品属性(销售类型/基本类型)以及商品分类进行查询", notes = "查询商品某种具体属性的参数，可根据关键字等查询")
    public R bashlist(@RequestParam(required = false) Map<String, Object> params,  @PathVariable String attrType,@PathVariable Long catId){

        PageUtils page = attrService.queryBasePage(params,catId,attrType);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @ApiOperation(value = "根据商品属性id查询属性接口",notes = "不仅仅会查询属性，还会查询属性的关联表")
    @GetMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrRespVo attr = attrService.getAttrInfor(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存商品属性接口",notes = "不仅仅会保存属性，还会保存属性的关联表")
    @PostMapping("/save")
    public R save(@RequestBody AttrVo attr){
//		attrService.save(attr);
        attrService.saveAttr(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改商品属性接口",notes = "不仅仅会修改属性，还会修改属性的关联表")
    @PostMapping("/update")
    public R update(@RequestBody AttrVo attr){
//		attrService.updateById(attr);
        System.out.println(attr);
        attrService.updateAttr(attr);
        return R.ok();
    }

    @ApiOperation(value = "更新商品规格参数",notes = "更新商品规格参数")
    @PostMapping("/update/{spuid}")
    public R updateSpuAttr(@RequestBody List<ProductAttrValueEntity> attrs, @PathVariable Long spuid){

        productAttrValueService.updateSpuAttr(spuid,attrs);
        return R.ok();
    }
    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
